package com.th.supcom.pds.recipefee.client.widget;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SpinnerField;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.google.gwt.i18n.client.NumberFormat;
import com.th.supcom.pds.common.utils.ClientUtils;

/**
 * 添加药袋窗口
 * 
 * @author zli
 */
public class BagWindow extends Window
{

    private SpinnerField m_spinnerField;
    public static final EventType feedBack = new EventType ();
    public static final EventType cancelBack = new EventType ();
    public boolean hasSave = false;

    public BagWindow ()
    {
        init ();
    }

    private void init ()
    {
        focus ();
        setResizable (false);
        setHeading ("添加塑料袋");
        setSize (400, 200);
        setModal (true);
        setBlinkModal (true);
        setLayout (new CenterLayout ());
        FormPanel simple = new FormPanel ();
        simple.setWidth ("350");
        simple.setBorders (false);
        simple.setHeaderVisible (false);
        add (simple);

        m_spinnerField = new SpinnerField ();
        m_spinnerField.focus ();
        m_spinnerField.setWidth ("300");

        m_spinnerField.setValue (1);
        m_spinnerField.setIncrement (1.00);
        m_spinnerField.getPropertyEditor ().setType (Double.class);
        m_spinnerField.getPropertyEditor ().setFormat (NumberFormat.getFormat ("0"));
        m_spinnerField.setMinValue (1.00);
        m_spinnerField.setMaxValue (100.00);
        m_spinnerField.setFieldLabel ("请输入数量");
        addKeyListenerNumber (m_spinnerField);
        simple.add (m_spinnerField);

        Button okButton = new Button ("确定");
        okButton.setWidth (80);
        okButton.setIntStyleAttribute ("margin-left", 200);
        okButton.setIntStyleAttribute ("margin-top", 10);
        addSelectionListenerOkButton (okButton);

        Button noButton = new Button ("取消");
        noButton.setWidth (80);
        noButton.setIntStyleAttribute ("margin-top", 10);
        addSelectionListenerOnButton (noButton);
        addButton (okButton);
        addButton (noButton);
        setFocusWidget (m_spinnerField);

    }


    private void addSelectionListenerOkButton (Button okButton)
    {
        okButton.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (ButtonEvent ce)
            {
                validateType ();
            }
        });
    }

    private void addSelectionListenerOnButton (Button noButton)
    {
        noButton.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (ButtonEvent ce)
            {
                BagWindow.this.hide ();
            }

        });
    }

    private void addKeyListenerNumber (final SpinnerField spinnerField)
    {
        spinnerField.addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (ComponentEvent event)
            {
                super.componentKeyDown (event);
                if (event.getKeyCode () == 13)
                {// 回车
                    validateType ();
                }

            }
        });

    }

    private void confirm ()
    {

        BagWindow.this.hide ();
        //插入一行
        AppEvent bagWindowEvent = new AppEvent (BagWindow.cancelBack);
        bagWindowEvent.setData ("number",m_spinnerField.getValue ());
        fireEvent (BagWindow.cancelBack, bagWindowEvent);
    }



    /**
     * 验证输入数字的额有效性
     * 
     * add by Zli
     */
    private void validateType ()
    {
        if ((Double) m_spinnerField.getValue () <= 0.0)
        {
            ClientUtils.alertMessageBox ("请输入正确的数量。", m_spinnerField);
            return;
        }
        if ((Double) m_spinnerField.getValue () > 100.0)
        {
            ClientUtils.alertMessageBox ("最多只能购买100个塑料袋。", m_spinnerField);
            return;
        }
        confirm ();
    }
}
